package com.example.config;

import com.example.interceptor.AuthenticationInterception;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 网络配置
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {
    // 跨域处理
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowCredentials(true)
                .allowedMethods("*")
                // 前端能带什么请求头
                .allowedHeaders("*")
                // 后端能返回什么请求头
                .exposedHeaders("*");
    }

    // 添加拦截器
    @Autowired
    AuthenticationInterception interceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(interceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/static/**");
    }
}
